package cn.zch.soft.util.file.delete;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Files;
/**
 * @author zch
 */
@Slf4j
public class FileDelete {
  private FileDelete() {
  }
  public static Integer delFile(String path) {
    try {
      File file = new File(path);
      if (file.isDirectory()) {
        return 0;
      }
      Files.delete(file.toPath());
      //系统进行资源强制回收
      return 1;
    }
    catch (Exception e) {
      // System.gc() Don't try to be smarter than the JVM, remove this call to run the garbage collector.
      return -1;
    }
  }
  /**
   * 删除文件夹下的所有文件
   *
   * @param path String
   * @return Integer
   * 0:path 不是文件夹
   * 1:删除成功
   * -1:删除失败 有异常
   */
  public static Integer delFilesInDictionary(String path) {
    try {
      File file = new File(path);
      if (!file.isDirectory()) {
        return 0;
      }
      File[] files = file.listFiles();
      if (files != null) {
        for (File f : files) {
          delFile(f.getPath());
        }
      }
      return 1;
    }
    catch (Exception e) {
      return -1;
    }
  }
}
